<template>
  <div class="container">
    <span>定时器状态：{{isRunning ? '运行中' : "已停止"}}</span>

    <data-view
      ref="dv"
      propViewKey="sys_schedule_job"
      propViewForm="sys/schedule-job/detail.vue"
      :propFlowKey="propFlowKey"
      @after-load="afterLoad"
      @extra-button-click="handleExtraButtonClick"
      :propIsTdesign="true"
      :propTableOpWidth="300"
    />

    <!--日志查看-->
    <tableDrawer
      ref="sysLogJob"
      refName="sysLogJob"
      viewKey="sys_log_job"
      :title="`执行日志【${selectJob.jobName}】`"
      attach="body"
      :widthPer="80"
      :propUserParam="cpLogPropUserParam"
      loadForce
      propOnlyQueryAll
    />
  </div>
</template>

<script>
/* eslint-disable */
export default {
  name: 'schedulejob',
  data() {
    return {
      isRunning: false,
      selectJob: {},
    }
  },
  computed: {
    // 调用日志查询条件
    cpLogPropUserParam() {
      let param = {}
      if (!this.selectJob.id) {
        param.busIdOr = -99
      }else{
        param.busIdOr = this.selectJob.id
        param.busKeyOr = this.selectJob.jobName
      }
      return param
    },
  },
  methods: {
    menuActiveCall() {
      this.$refs.dv.fetchData()
    },
    handleExtraButtonClick(extra) {
      let {button: {key}, data} = extra
      if (key === 'startAll') {
        this.startAll()
      } else if (key === 'stopAll') {
        this.stopAll()
      } else if (key === 'resume') {
        this.resume(data)
      } else if (key === 'pause') {
        this.pause(data)
      } else if (key === 'runOnce') {
        this.runOnce(data)
      } else if (key === 'execMore') {
        this.execMore()
      } else if (key === 'clearMscmToken') {
        this.clearMscmToken()
      } else if (key === 'disabledBatch') { // 批量禁用
        this.disabledBatch()
      } else if (key === 'enabledBatch') { // 批量启用
        this.enabledBatch()
      } else if (key === 'looklog') { // 查看日志
        this.selectJob = data
        this.$refs.sysLogJob.show()
      }
    },
    clearMscmToken() {
      this.$ajax.post('/sys/schedule_job/clearMscmToken').then(resp => {
        this.utilMessage.success(resp.msg)
      })
    },
    startAll() {
      this.utilVue.loadShow(true)
      this.$ajax.post('/sys/schedule_job/startAll').then(resp => {
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
        } else {
        }
        this.$refs.dv.fetchData()
        this.utilVue.loadHide()
        this.getIsRunning()
      })
    },
    stopAll() {
      this.utilVue.loadShow(true)
      this.$ajax.post('/sys/schedule_job/stopAll').then(resp => {
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
        } else {
        }
        this.$refs.dv.fetchData()
        this.utilVue.loadHide()
        this.getIsRunning()
      })
    },
    resume(row) {
      this.utilVue.loadShow(true)
      this.$ajax.post(`/sys/schedule_job/resume/${row.id}`).then(resp => {
        if (resp.code == 0)  this.utilMessage.success(resp.msg)

        this.$refs.dv.fetchData()
        this.utilVue.loadHide()
      })
    },
    pause(row) {
      this.utilVue.loadShow(true)
      this.$ajax.post(`/sys/schedule_job/pause/${row.id}`).then(resp => {
        if (resp.code == 0)  this.utilMessage.success(resp.msg)
        this.$refs.dv.fetchData()
        this.utilVue.loadHide()
      })
    },
    runOnce(row) {
      this.$confirm('确认执行【' + row.jobName + '】任务！！！<br><small>你可以等待返回结果或者自行查看调用日志</small>?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        this.doRunOnce(row)
      }).catch(() => {
      })
    },
    doRunOnce(row) {
      this.utilVue.loadShow(true)
      this.$ajax.post(`/sys/schedule_job/runOnce/${row.id}`).then(resp => {
        this.utilVue.loadHide()
        if (resp.code == 0)  {
          global.utilMessage.alertSuccess(resp.msg,  () => {
            this.$refs.dv.fetchData()
          })
        }
      })
    },
    execMore() {
      let rows = this.$refs.dv.$refs.tbMain.getCheckboxRecords()
      if (!rows || rows.length < 1) {
        this.utilMessage.warning('请勾选需要执行的定时任务')
        return
      }
      let ids = []
      let names = []
      rows.forEach(ob => {
        ids.push(ob.id)
        names.push(ob.jobName)
      })

      this.$confirm(`是否执行这些定时任务，数量：${names.length}？<br><div style="word-break: break-all;">${names.join('<br>')}</div> `, '提示', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.utilVue.loadShow(true)
        this.$ajax.post('/sys/schedule_job/execMore', {'ids': ids.join(',')}, {timeout: 1000 * 60 * 30}).then(resp => {
          this.utilVue.loadHide()
          if (resp.code == 0)  {
            this.utilMessage.success(resp.msg, 10000)
          }
        })
      }).catch(() => {
      })
    },
    // 批量禁用
    disabledBatch() {
      let rows = this.$refs.dv.$refs.tbMain.getCheckboxRecords()
      if (!rows || rows.length < 1) {
        this.utilMessage.warning('请勾选需要禁用的定时任务')
        return
      }
      let ids = []
      let names = []
      rows.forEach(ob => {
        ids.push(ob.id)
        names.push(ob.jobName)
      })

      this.$confirm(`是否禁用这些定时任务，数量：${names.length}？<br><div style="word-break: break-all;">${names.join('<br>')}</div> `, '提示', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.utilVue.loadShow(true)
        this.$ajax.post('/sys/schedule_job/disabledBatch', {'ids': ids.join(',')}, {timeout: 1000 * 60 * 30}).then(resp => {
          this.utilVue.loadHide()
          if (resp.code == 0)  {
            this.utilMessage.success(resp.msg)
            this.$refs.dv.fetchData()
          }
        })
      }).catch(() => {
      })
    },
    // 批量启用
    enabledBatch() {
      let rows = this.$refs.dv.$refs.tbMain.getCheckboxRecords()
      if (!rows || rows.length < 1) {
        this.utilMessage.warning('请勾选需要启用的定时任务')
        return
      }
      let ids = []
      let names = []
      rows.forEach(ob => {
        ids.push(ob.id)
        names.push(ob.jobName)
      })

      this.$confirm(`是否启用这些定时任务，数量：${names.length}？<br><div style="word-break: break-all;">${names.join('<br>')}</div> `, '提示', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        this.utilVue.loadShow(true)
        this.$ajax.post('/sys/schedule_job/enabledBatch', {'ids': ids.join(',')}, {timeout: 1000 * 60 * 30}).then(resp => {
          this.utilVue.loadHide()
          if (resp.code == 0)  {
            this.utilMessage.success(resp.msg)
            this.$refs.dv.fetchData()
          }
        })
      }).catch(() => {
      })
    },

    getIsRunning() {
      this.$ajax.get('/sys/schedule_job/isRunning').then(resp => {
        this.isRunning = resp.data.isRunning
      })
    },

    afterLoad() {
      this.getIsRunning()
    }
  },
  created() {
    this.getIsRunning()
  },
  props: ['propFlowKey'],
}
</script>

<style lang="scss" scoped>
  .container {
    height: 100%;
    background-color: #fff;
  }
</style>
